package com.yzd.learnjava.holding;

import java.awt.datatransfer.StringSelection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

import com.yzd.learnjava.util.Print;

public class AdapterMethodIdiom {

	public static void main(String[] args) {
		ReversibleArrayList<String> ral = 
				new ReversibleArrayList(Arrays.asList("To be or not to be".split(" ")));
		
		for(String s : ral) {
			Print.printnb(s + " ");
		}
		
		Print.print();
		
		for(String s : ral.reversed()) {
			Print.printnb(s + " ");
		}
		
	}

}

class ReversibleArrayList<T> extends ArrayList<T>{
	public ReversibleArrayList(Collection<T> c) {
		super(c);
	}
	
	public Iterable<T> reversed(){
		return new Iterable<T>() {
			
			public Iterator<T> iterator() {
				return new Iterator<T>() {
					int current = size() - 1;
					public boolean hasNext() {return current > -1;}
					public T next() {
						return get(current--);
					}
					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
				
			}
		};
	}
}
